package viettravel.guiandevent.map;

import java.util.ArrayList;

import org.holoeverywhere.widget.ListView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockListActivity;
import com.actionbarsherlock.view.MenuItem;
import viettravel.DAO.LocalMapDAO;
import viettravel.common.Constants;
import viettravel.guiandevent.R;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

public class LocationListActivity extends SherlockListActivity {
	private ListView listLocation;
	private ArrayList<LocalMapDAO> list;
	private ListMoveMapAdapter adaptor;
	private String jsonString;
	private int key;

	private Double mLat;
	private Double mLong;
	private String mAddress;
	private String location;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTheme(Constants.THEME);

		ActionBar actionBar = getSupportActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
		actionBar.setDisplayHomeAsUpEnabled(false);
		actionBar.setHomeButtonEnabled(true);
		actionBar.setIcon(R.drawable.ic_back_arrow);
		actionBar.setBackgroundDrawable(Constants.APP_DRAWABLE);
		actionBar.setTitle("List location");

		setContentView(R.layout.activity_location_list);
		listLocation = (ListView) findViewById(android.R.id.list);
		listLocation.setOnItemClickListener(itemClickListener);
		list = new ArrayList<LocalMapDAO>();
		Bundle extra = getIntent().getExtras();
		if (extra == null)
			return;
		try {
			jsonString = extra.getString(Constants.LOCATIONLIST);
			key = extra.getInt(Constants.LOCATION_KEY);
			location = extra.getString(Constants.MY_LOCATION);
		} catch (Exception e) {

		}
		list = jsonToListAddress(jsonString, key);
		adaptor = new ListMoveMapAdapter(getBaseContext(), list);
		listLocation.setAdapter(adaptor);

	}

	OnItemClickListener itemClickListener = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int itemId,
				long groupId) {
			LocalMapDAO localMapDao = list.get(itemId);
			mLat = localMapDao.getLatitude();
			mLong = localMapDao.getLongtitude();
			mAddress = localMapDao.getAddress();
			Intent intent = new Intent(LocationListActivity.this,
					GetDirectionDialog.class);
			intent.putExtra(Constants.MY_LOCATION, location);
			intent.putExtra(Constants.PLACE_WANT_SEARCH, mLat.toString() + ","
					+ mLong.toString());
			intent.putExtra(Constants.TAG_PLACES_NAME, mAddress);
			startActivity(intent);

		}
	};

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home: {
			finish();
			return true;
		}
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private ArrayList<LocalMapDAO> jsonToListAddress(String str_places, int key) {
		try {
			ArrayList<LocalMapDAO> places = new ArrayList<LocalMapDAO>();
			JSONArray jsonPlaces = new JSONArray(str_places);
			for (int i = 0; i < jsonPlaces.length(); i++) {
				JSONObject jsonPlace = (JSONObject) jsonPlaces.get(i);
				JSONObject jsonLatLng = new JSONObject(
						jsonPlace.getString(Constants.TAG_LATLNG));
				LocalMapDAO place = new LocalMapDAO(null,
						jsonPlace.getString(Constants.TAG_NAME),
						jsonPlace.getString(Constants.TAG_LADDR),
						jsonLatLng.getDouble(Constants.TAG_LAT),
						jsonLatLng.getDouble(Constants.TAG_LNG));
				places.add(place);
			}
			return places;
		} catch (JSONException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;
	}

}
